﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;

public partial class Main_ServiceInvoice_InvoiceSettings : System.Web.UI.Page
{
    string strsql = string.Empty;
    Datautility DU = new Datautility();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            _BindTdsHeadAccounts();
            _BindWriteOffAccounts();
            _BindRoundOffSetting();
            _BindGeneralSettings();
        }
    }
    private void _BindRoundOffSetting()
    {
        DataTable DTRoundOff = DU.getdatausingprocedure("USP_IVC_GetRoundOffSettings");
        if (DTRoundOff.Rows.Count > 0)
        {
            ddlRoundST.SelectedValue = DTRoundOff.Rows[0]["ROUNDOFFOPTION"].ToString();//ROUNDOFFID 1
            ddlRoundPECess.SelectedValue = DTRoundOff.Rows[1]["ROUNDOFFOPTION"].ToString();//ROUNDOFFID 2
            ddlRoundHECEss.SelectedValue = DTRoundOff.Rows[2]["ROUNDOFFOPTION"].ToString();//ROUNDOFFID 3
            ddlRoundLuxuryTax.SelectedValue = DTRoundOff.Rows[3]["ROUNDOFFOPTION"].ToString();//ROUNDOFFID 4
            ddlRoundAmtRec.SelectedValue = DTRoundOff.Rows[4]["ROUNDOFFOPTION"].ToString();//ROUNDOFFID 5
        }
    }
    private void _BindGeneralSettings()
    {
        string strFinYear = string.Empty;
        if (Session["FINYEAR"] != null)
        {
            strFinYear = Session["FINYEAR"].ToString();
        }
        if (strFinYear != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@FINYEAR",strFinYear)
                                   };
            DataTable DTGeneralSetting = DU.getdatausingprocedure("USP_IVC_GetGeneralSettings", sparam);
            if (DTGeneralSetting.Rows.Count > 0)
            {
                chkGenerateST3.Checked = Convert.ToBoolean(DTGeneralSetting.Rows[0]["GENERATEST3"]);
                txtServiceTaxThreshold.Text = string.Format("{0:0.00}", DTGeneralSetting.Rows[1]["ACCOUNTINGMETHOD"]).ToString();
                txtPostBilling.Text = string.Format("{0:0.00}", DTGeneralSetting.Rows[1]["GENERATEST3"]).ToString();
                ddlDecimalPlaces.SelectedValue = DTGeneralSetting.Rows[0]["DECIMALPLACES"].ToString();
                ddlTDSAccounts.SelectedValue = DTGeneralSetting.Rows[0]["TDS_LEDGERID"].ToString();
                ddlWriteOffAccount.SelectedValue = DTGeneralSetting.Rows[0]["WRITEOFF_LEDGERID"].ToString();
            }
        }
    }
    private void _SaveGeneralSettingOption()
    {
        try
        {
            string strFinYear = string.Empty;
            if (Session["FINYEAR"] != null)
            {
                strFinYear = Session["FINYEAR"].ToString();
            }
            if (strFinYear != string.Empty)
            {
                SqlParameter[] sparam =
                                {
                                    new SqlParameter("@ACCMETHOD","1"),
                                    new SqlParameter("@DECIMALPLACES",ddlDecimalPlaces.SelectedValue),
                                    new SqlParameter("@GENERATEST3",chkGenerateST3.Checked),
                                    new SqlParameter("@FINYEAR",strFinYear),
                                    new SqlParameter("@THRESHOLDLIMIT",txtServiceTaxThreshold.Text),
                                    new SqlParameter("@POSTBILL_LIMT",txtPostBilling.Text),
                                    new SqlParameter("@WRITEOFF_LEDGERID",ddlWriteOffAccount.SelectedValue),
                                    new SqlParameter("@TDS_LEDGERID",ddlTDSAccounts.SelectedValue)
                                };
                DU.executeprocedure("USP_IVC_SaveGeneralSettings", sparam);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveRoundOffSettings()
    {
        try
        {
            SqlParameter[] sparam =
                                {
                                    new SqlParameter("@ROUNDOFFID","1"),
                                    new SqlParameter("@ROUNDOFFOPTION",ddlRoundST.SelectedValue)                                    
                                };
            DU.executeprocedure("USP_IVC_SaveRoundOffSettings", sparam);
            SqlParameter[] sparam1 =
                                {
                                    new SqlParameter("@ROUNDOFFID","2"),
                                    new SqlParameter("@ROUNDOFFOPTION",ddlRoundPECess.SelectedValue)                                    
                                };
            DU.executeprocedure("USP_IVC_SaveRoundOffSettings", sparam1);
            SqlParameter[] sparam2 =
                                {
                                    new SqlParameter("@ROUNDOFFID","3"),
                                    new SqlParameter("@ROUNDOFFOPTION",ddlRoundHECEss.SelectedValue)                                    
                                };
            DU.executeprocedure("USP_IVC_SaveRoundOffSettings", sparam2);
            SqlParameter[] sparam3 =
                                {
                                    new SqlParameter("@ROUNDOFFID","4"),
                                    new SqlParameter("@ROUNDOFFOPTION",ddlRoundLuxuryTax.SelectedValue)                                    
                                };
            DU.executeprocedure("USP_IVC_SaveRoundOffSettings", sparam3);
            SqlParameter[] sparam4 =
                                {
                                    new SqlParameter("@ROUNDOFFID","5"),
                                    new SqlParameter("@ROUNDOFFOPTION",ddlRoundAmtRec.SelectedValue)                                    
                                };
            DU.executeprocedure("USP_IVC_SaveRoundOffSettings", sparam4);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _SaveRoundOffSettings();
                _SaveGeneralSettingOption();
                lblMsg.Text = "Settings Updated..";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    private void _BindTdsHeadAccounts()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TYPE","0")

                                };
        DataTable DT = DU.getdatausingprocedure("USP_IVC_GetTDSAccountLedgerForSettings", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlTDSAccounts.DataSource = DT;
            ddlTDSAccounts.DataTextField = "LNAME";
            ddlTDSAccounts.DataValueField = "LID";
            ddlTDSAccounts.DataBind();
        }
    }
    private void _BindWriteOffAccounts()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TYPE","1")

                                };
        DataTable DT = DU.getdatausingprocedure("USP_IVC_GetTDSAccountLedgerForSettings", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlWriteOffAccount.DataSource = DT;
            ddlWriteOffAccount.DataTextField = "LNAME";
            ddlWriteOffAccount.DataValueField = "LID";
            ddlWriteOffAccount.DataBind();
        }
    }
}

   